/// <reference path="../typings/globals/jquery/index.d.ts" />

'use strict'
var Configure = require('./configure.js')

function activateElement(elements, toActivate, classTag) {
    elements.removeClass(classTag)
    toActivate.classList.add(classTag)
}

function getActivatedTab() {
    let $tabs = $('.tab-wrapper>input[type=radio]')
    for (let i = 0; i < $tabs.length; i++) {
        if ($tabs[i].checked) {
            return $tabs[i]
        }
    }

    return null
}

function getActivatedCheckbox() {
    let checked = getActivatedTab()

    if (checked.id == 'tab-radio-1') {
        return $('#cad-version-config input[type=checkbox]')
    } else if (checked.id == 'tab-radio-2') {
        return $('#plugin-target input[type=checkbox]')
    }
}

function onCheckboxClick(element, checked) {
    let parent = element

    let parentLv = Number(parent.getAttribute('level'))
    // 当前点击的checkbox所在div是父级，其子级div包含的checkbox全部勾选/取消勾选
    if (parent.hasAttribute('Out')) {
        return
    }

    let sib = parent
    do {
        sib = sib.nextElementSibling
        let sibLv = Number(sib.getAttribute('level'))
        if (sibLv <= parentLv) {
            break
        }

        let childCheckbox = sib.getElementsByTagName('input')
        childCheckbox[0].checked = checked
    } while (sib.nextElementSibling != null);
}

function onMoveUp() {
    let $toremove = $('#plugin-target>.activity')
    let $prev = $toremove.prev() // 激活节点的上一个节点
    if ($prev.length == 0 || $prev.attr('Out') === undefined) {
        return
    }

    let $activated = $toremove.clone(true) // 拷贝节点所有数据
    $toremove.remove() // 删除被克隆节点
    $activated.each((i, e) => {
        document.getElementById('plugin-target').insertBefore(e, $prev[0])
    })
}

function onMoveDown() {
    let $toremove = $('#plugin-target>.activity')
    let $next = $toremove.next() // 激活节点的下一个节点
    if ($next.length == 0 || $next.attr('Out') === undefined) {
        return
    }

    let $activated = $toremove.clone(true) // 拷贝节点所有数据
    $toremove.remove() // 删除被克隆节点
    $activated.each((i, e) => {
        document.getElementById('plugin-target').insertBefore(e, $next[0].nextElementSibling)
    })
}

exports.divClick = function() {
    return {
        click: event => {
            let obj = event.currentTarget
            if (getActivatedTab().id == 'tab-radio-2') {
                activateElement($('#plugin-target > div'), obj, 'activity')
            }
        }
    }
}

exports.checkboxClick = function() {
    return {
        click: event => {
            let obj = event.currentTarget
            onCheckboxClick(obj.parentElement, obj.checked)
        }
    }
}

exports.buttonClick = function() {
    return {
        click: event => {
            // 获得当前选择tab页签
            var $activated = getActivatedCheckbox()

            let obj = event.currentTarget
            if (obj.id === 'select-all' && $activated) { // 点击全选
                $activated.each((i, e) => {
                    e.checked = true
                })
            } else if (obj.id === 'select-other' && $activated) { // 点击反选
                $activated.each((i, e) => {
                    e.checked = !e.checked
                })
            } else if (obj.id === 'moveup') { // 点击上移
                onMoveUp()
            } else if (obj.id === 'movedown') { // 点击下移
                onMoveDown()
            } else if (obj.id === 'submit') {   // 点击提交按钮，提交信息到服务开始打包
                Configure.save()
                Configure.request()
            }
        }
    }
}

exports.progressExpand = function(){
    return {
        click: event => {
            let obj = event.currentTarget
            let log = obj.getElementsByClassName('log').item(0)

            if (log.classList.contains('visible')){
                log.classList.remove('visible')
            } else {
                 activateElement($('#build-status .log'), log, 'visible')
            }
        }
    }
}
